package com.aaa.backsystem.controller;

import com.aaa.backsystem.entity.LayuiTable;
import com.aaa.backsystem.entity.Result;
import com.aaa.backsystem.entity.WxTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

/**
 * fileName:TaskController
 * description:
 * author:gyc
 * createTime:2020/8/22 11:03
 * version:1.0.0
 */
@Controller
@RequestMapping("/task")
public class TaskController extends BaseController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/toTask")
    public String toTask(){
        return "task/task";
    }

    /**
     * 查询任务列表
     * @return
     */
    @GetMapping("/findTask")
    @ResponseBody
    public LayuiTable findTask(Integer page,Integer limit){
        /**
         * 调用微信端接口方法
         * String.class：返回值类型
         */
        LayuiTable layuiTable = restTemplate.getForObject("http://localhost:9091/wxTask/findTask/{page}/{limit}", LayuiTable.class,page,limit);
        return layuiTable;
    }

    /**
     * 添加任务
     * @return
     */
    @PostMapping("/addTask")
    @ResponseBody
    public Result addTask(@RequestBody WxTask wxTask){
        /**
         * 调用微信端接口方法
         */
        return restTemplate.postForObject("http://localhost:9091/wxTask/addTask", wxTask, Result.class);
    }

    /**
     * 删除任务
     * @return
     */
    @DeleteMapping("/deleteTask/{id}")
    @ResponseBody
    public Result deleteTask(@PathVariable("id") Integer id){
        /**
         * 调用微信端接口方法
         */
        try {
            restTemplate.delete("http://localhost:9091/wxTask/deleteTask/{id}",id);
            return success();
        } catch (RestClientException e) {
            e.printStackTrace();
        }
        return error();
    }


}
